package hephaestus.server.proxy;

import java.io.IOException;
import java.net.SocketException;

import hephaestus.common.*;

public class ClientConnection {
	private GameServer currentGameServer;
	private Connection connection;
	private String username;
	private long authID;

	public final static long INVALID_AUTH_ID = 0;
	private static int CLIENT_TIMEOUT = 1; // milliseconds
	private static long nextAuthID = 1;
	
	public ClientConnection(String username, Connection connection)
	{
		this.connection = connection;
		try {
			this.connection.setTimeout(CLIENT_TIMEOUT);
		} catch (SocketException e) {
			e.printStackTrace();
		}
		currentGameServer = null;
		this.username = username;
		authID = nextAuthID ++;
	}
	
	public String getIP()
	{
		return connection.getIP();
	}
	
	public void setCurrentGameServer(GameServer gameServer)
	{
		currentGameServer = gameServer;
	}
	
	public boolean checkAuthID(String ID)
	{
		return (authID == Long.parseLong(ID));
	}
	
	public long getAuthID()
	{
		return authID;
	}
	
	public boolean isUser(String username)
	{
		return this.username.equals(username);
	}
	
	public void think() throws IOException
	{
		String message = connection.receive();
		if(message != null)
		{
			System.out.println("! Client: '" + message.trim() + "'");
		}
	}
}
